package com.hot100.mid;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 打劫家舍
 * @date 2022/9/4 13:16
 */
public class Demo37 {
    public int rob(int[] nums) {
        if (nums.length<=0) {
            return 0;
        }
        if (nums.length==1) {
            return nums[0];
        }
        if (nums.length==2) {
            return Math.max(nums[0],nums[1]);
        }
        if (nums.length==3) {
            return Math.max(nums[0]+nums[2],nums[1]);
        }
        int[] res=new int[nums.length];
        res[0]=nums[0];
        res[1]=nums[1];
        res[2]=nums[0]+nums[2];
        for (int i =3; i < res.length ; i++) {
            res[i]=Math.max(res[i-2],res[i-3])+nums[i];
        }
        return Math.max(res[res.length-1],res[res.length-2]);
    }
}
